<template>
  <div>
    <el-dialog
      v-loading.lock="loading"
      class="base-dialog system-user-dialog"
      :append-to-body="true"
      title="出入库订单产品详细信息"
      align="center"
      :close-on-click-modal="false"
      :visible.sync="innerDialogFormVisible"
    >
      <el-form
        ref="formData"
        class="base-pipeline-layout system-user-form"
        label-width="100px"
        :model="formData"
        :rules="formDataRules"
        status-icon
      >
        <el-form-item
          label="订单产品id"
          :label-width="formLabelWidth"
          prop="orderProductId"
          :error="errorMsg.orderProductId"
        >
          <el-input
          v-model="formData.orderProductId"
          autocomplete="off"
          type="number"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="订单id"
          :label-width="formLabelWidth"
          prop="orderId"
          :error="errorMsg.orderId"
        >
          <el-input
          v-model="formData.orderId"
          autocomplete="off"
          type="number"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="产品id"
          :label-width="formLabelWidth"
          prop="productId"
          :error="errorMsg.productId"
        >
          <el-input
          v-model="formData.productId"
          autocomplete="off"
          type="number"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="产品编号"
          :label-width="formLabelWidth"
          prop="productNum"
          :error="errorMsg.productNum"
        >
          <el-input
          v-model="formData.productNum"
          autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="入库数量/出库数量"
          :label-width="formLabelWidth"
          prop="productCount"
          :error="errorMsg.productCount"
        >
          <el-input
          v-model="formData.productCount"
          autocomplete="off"
          type="number"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="库区"
          :label-width="formLabelWidth"
          prop="storageAreaId"
          :error="errorMsg.storageAreaId"
        >
          <el-input
          v-model="formData.storageAreaId"
          autocomplete="off"
          type="number"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="库区Name"
          :label-width="formLabelWidth"
          prop="storageAreaName"
          :error="errorMsg.storageAreaName"
        >
          <el-input
          v-model="formData.storageAreaName"
          autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="库位"
          :label-width="formLabelWidth"
          prop="storageSpaceId"
          :error="errorMsg.storageSpaceId"
        >
          <el-input
          v-model="formData.storageSpaceId"
          autocomplete="off"
          type="number"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="库位Name"
          :label-width="formLabelWidth"
          prop="storageSpaceName"
          :error="errorMsg.storageSpaceName"
        >
          <el-input
          v-model="formData.storageSpaceName"
          autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="证书编号"
          :label-width="formLabelWidth"
          prop="certificateNum"
          :error="errorMsg.certificateNum"
        >
          <el-input
          v-model="formData.certificateNum"
          autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="批次编号"
          :label-width="formLabelWidth"
          prop="batchNum"
          :error="errorMsg.batchNum"
        >
          <el-input
          v-model="formData.batchNum"
          autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="供应商id"
          :label-width="formLabelWidth"
          prop="supplierId"
          :error="errorMsg.supplierId"
        >
          <el-input
          v-model="formData.supplierId"
          autocomplete="off"
          type="number"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="供应商Name"
          :label-width="formLabelWidth"
          prop="supplierName"
          :error="errorMsg.supplierName"
        >
          <el-input
          v-model="formData.supplierName"
          autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="生产日期"
          :label-width="formLabelWidth"
          prop="dateManufacture"
          :error="errorMsg.dateManufacture"
        >
          <el-input
          v-model="formData.dateManufacture"
          autocomplete="off"
          type="number"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button type="primary" @click="onSubmit('formData')"
          >保 存</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { create, update } from "../../../api/Material/MaterialOrderProductDetails.js";

export default {
  name: "MaterialOrderProductDetailsEdit",
  props: {
    dialogFormVisible: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      loading: false,
      innerDialogFormVisible: false,
      formLabelWidth: "100px",
      initData: {
        id: 0,
        orderProductId: 0,
        orderId: 0,
        productId: 0,
        productNum: "",
        productCount: 0,
        storageAreaId: 0,
        storageAreaName: "",
        storageSpaceId: 0,
        storageSpaceName: "",
        certificateNum: "",
        batchNum: "",
        supplierId: 0,
        supplierName: "",
        dateManufacture: 0,
      },
      formData: {
        id: 0,
        orderProductId: 0,
        orderId: 0,
        productId: 0,
        productNum: "",
        productCount: 0,
        storageAreaId: 0,
        storageAreaName: "",
        storageSpaceId: 0,
        storageSpaceName: "",
        certificateNum: "",
        batchNum: "",
        supplierId: 0,
        supplierName: "",
        dateManufacture: 0,
      },
      formDataRules: {
        orderProductId: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
        orderId: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
        productId: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
        productNum: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunction,
          },
        ],
        productCount: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
        storageAreaId: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
        storageAreaName: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunction,
          },
        ],
        storageSpaceId: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
        storageSpaceName: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunction,
          },
        ],
        certificateNum: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunction,
          },
        ],
        batchNum: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunction,
          },
        ],
        supplierId: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
        supplierName: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunction,
          },
        ],
        dateManufacture: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionPositiveNumber,
          },
        ],
      },
      initErrorMsg: {
        orderProductId: "",
        orderId: "",
        productId: "",
        productNum: "",
        productCount: "",
        storageAreaId: "",
        storageAreaName: "",
        storageSpaceId: "",
        storageSpaceName: "",
        certificateNum: "",
        batchNum: "",
        supplierId: "",
        supplierName: "",
        dateManufacture: "",
      },
      errorMsg: {
        orderProductId: "",
        orderId: "",
        productId: "",
        productNum: "",
        productCount: "",
        storageAreaId: "",
        storageAreaName: "",
        storageSpaceId: "",
        storageSpaceName: "",
        certificateNum: "",
        batchNum: "",
        supplierId: "",
        supplierName: "",
        dateManufacture: "",
      },
    };
  },
  watch: {
    dialogFormVisible(val) {
      this.innerDialogFormVisible = val;
    },
    innerDialogFormVisible(val) {
      this.$emit("update:dialogFormVisible", val);
    },
  },
  methods: {
    cancel() {
      this.innerDialogFormVisible = false;
    },
    closeDialog() {
      this.$parent.getList();
      this.cancel();
    },
    onSubmit() {
      this.$refs.formData.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.errorMsg = this.Helper.deepCopy(this.initErrorMsg);
          if (this.formData.id <= 0) {
            create(this.formData)
              .then((res) => {
                if (res.code === 200) {
                  this.closeDialog();
                } else {
                  this.showValidateMsgCheck(res);
                }
                this.loading = false;
              })
              .catch(() => {
                this.loading = false;
              });
          } else {
            update(this.formData)
              .then((res) => {
                if (res.code === 200) {
                  this.closeDialog();
                } else {
                  this.showValidateMsgCheck(res);
                }
                this.loading = false;
              })
              .catch(() => {
                this.loading = false;
              });
          }
        } else {
          return false;
        }
      });
    },
    showValidateMsgCheck(res) {
      if (res.msg) {
        this.Helper.showValidateMsg("orderProductId", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("orderId", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("productId", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("productNum", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("productCount", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("storageAreaId", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("storageAreaName", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("storageSpaceId", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("storageSpaceName", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("certificateNum", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("batchNum", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("supplierId", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("supplierName", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("dateManufacture", this.errorMsg, res.msg);
      }
    },
  },
};
</script>
